/*************************************************************************
 *
 * SensorTask
 * 
 * (c) Ales Povalac, alpov@alpov.net, http://www.alpov.net/
 * 
 *************************************************************************
 *
 * $Id: sensor.c 126 2007-02-10 14:57:23Z alpov $
 * 
 *************************************************************************/         

#include <string.h>
#include <avr/io.h>
#include "../avrx/avrx.h"
#include "../avrx/AvrXFifo.h"
#include "sensor.h"
#include "1wire.h"
#include "data.h"
#include "serial.h"
#include "log.h"

TimerControlBlock SensorDelayTimer;
Mutex SensorReady;


/*************************************************************************
 * uloha SensorTask
 * min. stack fixme
 *************************************************************************/ 
AVRX_GCC_TASKDEF(SensorTask, 100, 3)
{
    int i;
    char addr[8];
    unsigned int cd_minute = 0, cd_log_high = 0, cd_log_low = 0;
    
    while (1) {
        AvrXStartTimer(&SensorDelayTimer, SENSOR_DELAY);
        
        // spustit konverzi na vsech senzorech a ulozit do stdata
        AvrXWaitSemaphore(&OneWireMutex);
        OWConvertAll(1);
        for (i = 0; i < AvrXReadEEProm(&eedata.sensor_count); i++) {
            ReadEEPromBlock(addr, &eedata.sensors[i], sizeof(addr));
            OWReadTemperature(addr, &stdata.temperature[i]);
        }
        AvrXSetSemaphore(&OneWireMutex);
        
        // po kazdem mereni poslat vsem jednotkam zmerene teploty ze senzoru
        AvrXSetSemaphore(&SensorReady);
        Serial_STDATA_W(ITEM_ALL);
        
        // kazdou minutu poslat vsem slave jednotkam aktualni cas
        if (!cd_minute) {
            Serial_RWDATA_W(ITEM_RTC, ADDR_BROADCAST);
            cd_minute = 60000 / SENSOR_DELAY;
        }
        
        // logovani teplot s nizsi frekvenci
        // pokud zaloguje, vyresetne take vyssi frekvenci, takze nebudou dva stejne zaznamy po sobe
        if (!cd_log_low) {
            LogSensors(LOG_SENSOR_LOW);
            cd_log_low = AvrXReadEEPromWord(&eedata.log_period_low);
            cd_log_high = AvrXReadEEPromWord(&eedata.log_period_high);
        }
        
        // logovani teplot s vyssi frekvenci
        if (!cd_log_high) {
            LogSensors(LOG_SENSOR_HIGH);
            cd_log_high = AvrXReadEEPromWord(&eedata.log_period_high);
        }
        
        cd_minute--;
        cd_log_high--;
        cd_log_low--;
        
        AvrXWaitTimer(&SensorDelayTimer);
        
        // -------- DEBUG --------- FIXME
        stdata.state.led ^= 0x01;   // zacvicit se zelenou ledkou jednotek
        stdata.state.led ^= 0x02;   // zacvicit se zlutou ledkou jednotek
        // -------- DEBUG ---------
    }
}


/*************************************************************************
 * inicializace tasku
 *************************************************************************/ 
void InitSensorTask(void)
{
//  memset(SensorTaskStk, 0xA0A0, sizeof(SensorTaskStk));
    OWInit();
    AvrXSetSemaphore(&OneWireMutex);
    
    // FIXME: DEBUG
    stdata.state.led = 0x01;
}

